/*
 * LexiconReader.java
 *
 * Created on June 25, 2006, 10:18 PM
 *
 */

package galronnlp.io;

import galronnlp.cfg.lexicon.LexicalEntry;
import galronnlp.cfg.lexicon.malformedLexicalEntryException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Scanner;
import galronnlp.util.Symbol;
import galronnlp.util.SymbolGenerator;

/**
 * Creates <code>LexicalEntry</code> from text files of the following format:
 *
 * A :- a : p
 * A :- a
 *
 *
 * @author Daniel A. Galron
 */
public class LexiconReader {
    
    private BufferedReader reader;
    public boolean hasNext = true;
    private SymbolGenerator symbolGen;
    
    /** Creates a new instance of LexiconReader */
    public LexiconReader(String source, SymbolGenerator gen) {
        symbolGen = gen;
        try {
            reader = new BufferedReader(new FileReader(new File(source)));
        } catch(FileNotFoundException fnfe) {
            System.err.println("Error: File " + source + " not found");
        }        
    }

    public LexiconReader(String lexiconString, SymbolGenerator gen, boolean string) {
        symbolGen = gen;
        reader = new BufferedReader(new StringReader(lexiconString));
    }    
    
    /**
     * This method returns a new <code>LexicalEntry</code> object to represent the production read.
     * The <code>Rule</code> object is a <code>Word</code> object
     *
     * @return the production rule.
     */    
    public LexicalEntry readNext() throws malformedLexicalEntryException {
        String WordString = "";
        boolean pcfg = false;
        try {
            String category = "";
            String word = "";
            String wordTemp = "";
            double prob = 1.0;
            WordString = reader.readLine();
            if(WordString != null) {
                Scanner scan = new Scanner(WordString).useDelimiter("\\s*:-\\s*");
                // Make sure that the rule is properly formatted with a left hand
                // category and a right hand word
                if(WordString.contains(":-")) {
                    category = scan.next();
                    wordTemp = scan.next();
                    // test to see if there is a probability associated with this lexical item
                    if(wordTemp.contains(":")) {
                        scan = new Scanner(wordTemp).useDelimiter("\\s*:\\s*");
                        word = scan.next();
                        prob = scan.nextDouble();
                        pcfg = true;
                    } else {
                        word = wordTemp.trim();
                    }
                    if(pcfg) {
                        return new galronnlp.pcfg.lexicon.Word(symbolGen.create(category), symbolGen.create(word), prob);
                    } else {
                        return new galronnlp.cfg.lexicon.Word(symbolGen.create(category), symbolGen.create(word));
                    }
                } else {
                    throw new malformedLexicalEntryException(WordString);
                }
            } else {
                hasNext = false;
            }
        } catch(Exception e) {
            hasNext = false;
        }
        if(WordString != null)
            throw new malformedLexicalEntryException(WordString);
        return null;
    }
    
    
    
}
